package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwamExport;
import cn.e4j.modular.crm.entity.GwamT;
import cn.e4j.modular.crm.entity.GwamlT;
import cn.e4j.modular.crm.entity.GwanT;
import cn.e4j.modular.crm.mapper.GwamTMapper;
import cn.e4j.modular.crm.service.GwamTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-11
 */
@Service
public class GwamTServiceImpl extends ServiceImpl<GwamTMapper, GwamT> implements GwamTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwamDtos = this.baseMapper.listPage(page, dto);

        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwanList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwamDtos = this.baseMapper.listGwanPage(page, dto);

        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwahList(Dto dto) {

        List<Map<String, Object>> gwahDtos = this.baseMapper.getGwahList(dto);

        return gwahDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaoList(Dto dto) {

        List<Map<String, Object>> gwaoDtos = this.baseMapper.getGwaoList(dto);

        return gwaoDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwabList(Dto dto) {

        List<Map<String, Object>> gwabDtos = this.baseMapper.getGwabList(dto);

        return gwabDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaiList(Dto dto) {

        List<Map<String, Object>> gwaiDtos = this.baseMapper.getGwaiList(dto);

        return gwaiDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwajList(Dto dto) {

        List<Map<String, Object>> gwajDtos = this.baseMapper.getGwajList(dto);

        return gwajDtos;
    }

    @Transactional(readOnly = true)
    public GwamT getByKey(String GWAMENT, String GWAM001) {
        GwamT gwam = this.baseMapper.getByKey(GWAMENT, GWAM001);
        return gwam;
    }

    @Transactional
    @Override
    public void addGwam(GwamT gwamT, GwamlT gwamlT) {
        gwamT.setGWAMACTI("Y");
        gwamT.insert();
        gwamlT.insert();
    }

    @Transactional
    @Override
    public void editGwam(GwamT gwam, GwamlT gwaml) {
        UpdateWrapper<GwamT> gwamTUpdateWrapper = new UpdateWrapper<>();
        gwam.setGWAMDATE(LocalDate.now());
        gwam.setGWAMTIME(DateUtils.getTime());
        gwamTUpdateWrapper.eq("GWAMENT", gwam.getGWAMENT());
        gwamTUpdateWrapper.eq("GWAM001", gwam.getGWAM001());

        UpdateWrapper<GwamlT> gwamlTUpdateWrapper = new UpdateWrapper<>();
        gwamlTUpdateWrapper.eq("GWAMLENT", gwam.getGWAMENT());
        gwamlTUpdateWrapper.eq("GWAML001", gwam.getGWAM001());
        gwam.update(gwamTUpdateWrapper);
        gwaml.update(gwamlTUpdateWrapper);
        Dto dto = Dtos.newDto();
        dto.put("GWAM001", gwam.getGWAM001());
        dto.put("GWAMENT", gwam.getGWAMENT());
        dto.put("GWAM021", gwam.getGWAM021());
        dto.put("GWAM024", gwam.getGWAM024());
        this.baseMapper.editData(dto);
    }

    @Transactional
    @Override
    public void deleteGwam(GwamT gwam, GwamlT gwaml) {
        UpdateWrapper<GwamT> gwamTUpdateWrapper = new UpdateWrapper<>();
        gwamTUpdateWrapper.eq("GWAMENT", gwam.getGWAMENT());
        gwamTUpdateWrapper.eq("GWAM001", gwam.getGWAM001());

        UpdateWrapper<GwamlT> gwamlTUpdateWrapper = new UpdateWrapper<>();
        gwamlTUpdateWrapper.eq("GWAMLENT", gwam.getGWAMENT());
        gwamlTUpdateWrapper.eq("GWAML001", gwam.getGWAM001());

        gwam.delete(gwamTUpdateWrapper);
        gwaml.delete(gwamlTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatchGwam(List<GwamT> gwamTList) {
        for (GwamT gwamT : gwamTList) {
            UpdateWrapper<GwamT> gwamTUpdateWrapper = new UpdateWrapper<>();
            gwamTUpdateWrapper.eq("GWAMENT", gwamT.getGWAMENT());
            gwamTUpdateWrapper.eq("GWAM001", gwamT.getGWAM001());

            GwamlT gwamlT = new GwamlT();
            UpdateWrapper<GwamlT> gwamlTUpdateWrapper = new UpdateWrapper<>();
            gwamlTUpdateWrapper.eq("GWAMLENT", gwamT.getGWAMENT());
            gwamlTUpdateWrapper.eq("GWAML001", gwamT.getGWAM001());

            gwamT.delete(gwamTUpdateWrapper);
            gwamlT.delete(gwamlTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public GwanT getByGwan(String GWANENT, String GWAN001, String GWAN002) {
        GwanT gwan = this.baseMapper.getByGwan(GWANENT, GWAN001, GWAN002);
        return gwan;
    }

    @Transactional
    @Override
    public void addGwan(GwanT gwanT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwanT.setGWANENT(shiroUser.getDBC());
        gwanT.setGWANACTI("Y");
        gwanT.insert();
    }

    @Transactional
    @Override
    public void editGwan(GwanT gwan) {
        UpdateWrapper<GwanT> gwanTUpdateWrapper = new UpdateWrapper<>();
        gwanTUpdateWrapper.eq("GWANENT", gwan.getGWANENT());
        gwanTUpdateWrapper.eq("GWAN001", gwan.getGWAN001());
        gwanTUpdateWrapper.eq("GWAN002", gwan.getGWAN002());
        gwan.update(gwanTUpdateWrapper);
    }

    @Transactional
    @Override
    public void deleteGwan(GwanT gwan) {
        UpdateWrapper<GwanT> gwanTUpdateWrapper = new UpdateWrapper<>();
        gwanTUpdateWrapper.eq("GWANENT", gwan.getGWANENT());
        gwanTUpdateWrapper.eq("GWAN001", gwan.getGWAN001());
        gwanTUpdateWrapper.eq("GWAN002", gwan.getGWAN002());
        gwan.delete(gwanTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatchGwan(List<GwanT> gwanTList) {
        for (GwanT gwanT : gwanTList) {
            UpdateWrapper<GwanT> gwanTUpdateWrapper = new UpdateWrapper<>();
            gwanTUpdateWrapper.eq("GWANENT", gwanT.getGWANENT());
            gwanTUpdateWrapper.eq("GWAN001", gwanT.getGWAN001());
            gwanTUpdateWrapper.eq("GWAN002", gwanT.getGWAN002());
            gwanT.delete(gwanTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwasList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwasDtos = this.baseMapper.listGwasPage(page, dto);
        return gwasDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getProductList(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWAMENT", shiroUser.getDBC());
//        List<Dto> result = this.baseMapper.getProductList(dto);
        List<Dto> result = InvokeUtil.invokeBeanMethod("gwamTMapper,getProductList",dto);
        return result;
    }

    @Transactional(readOnly = true)
    public Dto getPicture(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("GWAMENT", shiroUser.getDBC());
        Dto result = this.baseMapper.getPicture(dto);
        return result;
    }

    @Transactional(readOnly = true)
    public List<GwamExport> getListExport(Dto dto) {

        List<GwamExport> gwamDtos = this.baseMapper.listExport(dto);

        return gwamDtos;
    }


    @Transactional(readOnly = true)
    public List<Dto> getGwan(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ENT", shiroUser.getDBC());
        List<Dto> result = this.baseMapper.getGwan(dto);
        for(Dto pDto : result){
            pDto.put("ENT", shiroUser.getDBC());
            List<Dto> list = this.baseMapper.getGwaj(pDto);
            pDto.put("list", list);
        }
        return result;
    }

}
